package com.vinaprime.primeschedule.presentation;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.vinaprime.primeschedule.businesslayer.VPTaskList_BL;
import com.vinaprime.primeschedule.entities.VPTaskList;

public class VPTaskListActivity extends Activity {
	
	TabHost tab;
	TextView txt;
	List<VPTaskList> listTasks;
    ListView listView;
    TaskListAdapter list_adapter;
    VPTaskList_BL taskList_BL;
    ApplicationInformation appInfo;
    String path;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vptask_list);
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		loadTabs();
		//********* Tab 1.Task *********//
		appInfo = new ApplicationInformation(this);
		path = appInfo.getDBPath();
		taskList_BL = new VPTaskList_BL(this,  path + "databases/");
		showListTask();
		//******* End Tab 1.Task *******//
		
		//Control View
	/*
		final View controlsView = findViewById(R.id.fullscreen_content_controls);
		Button btnOK = (Button)findViewById(R.id.button_ok);
		btnOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				controlsView.setVisibility(View.GONE);
			}
		});
*/
	
	}
	public void loadTabs()
	 {
		 tab=(TabHost) findViewById (android.R.id.tabhost);
		 tab.setup();
		 Drawable d=getResources().getDrawable(R.raw.background_top);  
		 tab.setBackgroundDrawable(d);
		 TabHost.TabSpec spec;
		 // tab1
		 spec=tab.newTabSpec("t1");
		 spec.setContent(R.id.tab1);
		 spec.setIndicator("1-Tasks");
		 tab.addTab(spec);
		 // tab2
		 spec=tab.newTabSpec("t2");
		 spec.setContent(R.id.tab2);
		 spec.setIndicator("2-Completed");
		 tab.addTab(spec);
		 
		 tab.setCurrentTab(0);
		 
	 }
	public void showListTask()
	{	 
		txt = (TextView)findViewById(R.id.textView1);
		Toast.makeText(getApplicationContext(), "size: " + taskList_BL.TotalSize(), Toast.LENGTH_LONG).show();
		try {
			listTasks = taskList_BL.getAllTaskList();
			
			listView = (ListView) findViewById(R.id.listView_task);
			list_adapter = new  TaskListAdapter(this,R.layout.activity_vptasklist_item,(ArrayList<VPTaskList>)listTasks);
			listView.setAdapter(list_adapter);
			
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			txt.setText(e.toString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			txt.setText(e.toString());
		}
		//Toast.makeText(getBaseContext(),  "  111  --- " + listContactGroup.size(), Toast.LENGTH_SHORT).show();
		
		

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vptask_list, menu);
		return true;
	}



	//----------------------ADAPTER---------------------------------------//
		 class TaskListAdapter extends ArrayAdapter<VPTaskList> {
			  
			  private ArrayList<VPTaskList> taskList;
			  int textViewResourceId;
			  public TaskListAdapter(Context context, int _textViewResourceId,ArrayList<VPTaskList> taskList) {
				  super(context, _textViewResourceId, taskList);
			   		this.taskList = new ArrayList<VPTaskList>();
			   		this.taskList.addAll(taskList);
			   		this.textViewResourceId = _textViewResourceId;
			  }
			  
			  private class ViewHolder {
				  TextView  id;
				  TextView name;
				  
			  }
			  
			  @Override
			  public View getView(int position, View convertView, ViewGroup parent) {
			  
				   ViewHolder holder = null;
				   if (convertView == null) {
					   LayoutInflater vi = (LayoutInflater)getSystemService(
					     Context.LAYOUT_INFLATER_SERVICE);
					   convertView = vi.inflate(textViewResourceId, null);
					  
					   holder = new ViewHolder();
					   holder.id = (TextView) convertView.findViewById(R.id.task_item_id);
					   holder.name = (TextView) convertView.findViewById(R.id.task_item_name1);
					   convertView.setTag(holder);
			
				   }
				   else {
					   holder = (ViewHolder) convertView.getTag();
				   }
				  
				   		VPTaskList task = taskList.get(position);
				   		holder.id.setText(String.valueOf(task.getVP_TASK_ID()));
				   		holder.name.setText(task.getVP_TASK_NAME());
				   		return convertView;
				   }
			  
			 }

}
